//
//  RegularExp.m
//  iSalesOA
//
//  Created by 赵 莲锋 on 13-7-10.
//  Copyright (c) 2013年 赵 莲锋. All rights reserved.
//

#import "RegularExp.h"
static BOOL isHaveDian = NO;
static CGFloat maxFloat = 10000000000.0f;
@implementation RegularExp

+(BOOL)isValidateABC123:(NSString *)text;
{
    NSString *textRegex = @"^[A-Za-z0-9]+$";
    NSPredicate *textTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",textRegex];
    return [textTest evaluateWithObject:text];
}

/*汉字验证 MODIFIED BY HELENSONG*/
+(BOOL)isValidateRealName:(NSString *)name
{
    NSString *alph = @"^[\u4E00-\u9FA5]+$";
    NSPredicate *realNameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", alph];
    
    return [realNameTest evaluateWithObject:name];
}

/*邮箱验证 MODIFIED BY HELENSONG*/
+(BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

/**
 国际号码验证

 @param mobileNumbel 要验证的号码
 @return YES通过  NO不通过
 */
+ (BOOL) isMobileGuoJi:(NSString *)mobileNumbel{
    
    NSString *aaa = @"^\\s*\\+?\\s*(\\(\\s*\\d+\\s*\\)|\\d+)(\\s*-?\\s*(\\(\\s*\\d+\\s*\\)|\\s*\\d+\\s*))*\\s*$";
    
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", aaa];
    if (([regextestct evaluateWithObject:mobileNumbel]
         )) {
        return YES;
    }
    return NO;
}

/*手机号码验证 MODIFIED BY HELENSONG*/
+(BOOL)isValidateMobile:(NSString *)mobile
{    
    NSString *phoneRegex = @"^[1][0-9][0-9]{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
/*车牌号验证 MODIFIED BY HELENSONG*/
+(BOOL)validateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
//    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}

/*数字验证(只能输入数字)*/
+(BOOL)isValidateNum:(NSString *)number
{
    NSString *numRegex = @"^[0-9]*$";
    NSPredicate *numTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numRegex];
    return [numTest evaluateWithObject:number];
}
/*字母验证*/
+(BOOL)isValidateLetter:(NSString *)word
{
    NSString *regex = @"^[A-Za-z]+$";
    NSPredicate *numTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [numTest evaluateWithObject:word];
}
/*用户名验证*/
+(BOOL)isValidateLoginNum:(NSString *)loginNum{
    NSString *num = @"^[A-Z][A-Z0-9_]{0,19}$";
    NSPredicate *numTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",num];
//    NSLog(@"loginNum is %@",numTest);
    return [numTest evaluateWithObject:loginNum];
}

/*密码验证*/
+(BOOL)isValidatePassword:(NSString *)passWord{
    //以字母开头，只能包含“字母”，“数字”，“符号”，长度8~32
    NSString *regex = @"^(?=.*[a-zA-Z0-9].*)(?=.*[a-zA-Z\\W].*)(?=.*[0-9\\W].*).{8,32}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:passWord];
}


//身份证号
+ (BOOL) isValidateCardID: (NSString *)cardID
{
    BOOL flag;
    if (cardID.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:cardID];
}

/*
//身份证号码验证
+(BOOL)isValidateCardID:(NSString *)cardID
{
    //15位判断
    NSString *str15 = @"/^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/";
    NSPredicate *test15 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",str15];
    BOOL flag15 = [test15 evaluateWithObject:cardID];
    
    //18位判断
    NSString *str18 = @"/^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$/";
    NSPredicate *test18 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",str18];
    BOOL flag18 = [test18 evaluateWithObject:cardID];
    return (flag15 || flag18);
}
*/
 
/*验证联通号码*/
+(BOOL)isValidateUnionNumber:(NSString *)number{
//    NSString *num = @"^1(3[0-2]|4[5]|5[56]|8[56])\\d{8}$";
    NSString *num = @"^(?:13[0-2]|145|15[56]|176|18[56])\\d{8}$";
    NSPredicate *numTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",num];
    return [numTest evaluateWithObject:number];
}

/*大写字母验证*/
+(BOOL)isValidateUpAlphabet:(NSString *)alphabet{
    NSString *alph = @"[A-Z]";
    NSPredicate *alphTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",alph];
    return [alphTest evaluateWithObject:alphabet];
}

/*小写字母验证*/
+(BOOL)isValidateLowAlphabet:(NSString *)alphabet{
    NSString *alph = @"[a-z]";
    NSPredicate *alphTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",alph];
    return [alphTest evaluateWithObject:alphabet];
}

/*数字验证*/
+(BOOL)isvalidateNumber:(NSString *)number{
    NSString *numRegex = @"[0-9]";
    NSPredicate *numTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numRegex];
    return [numTest evaluateWithObject:number];
}

/*特殊符号验证*/
+(BOOL)isValidateSymbol:(NSString *)symbol{
    NSString *sym = @"[^a-zA-Z0-9]";
    NSPredicate *symTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",sym];
    return [symTest evaluateWithObject:symbol];
}
+ (BOOL)controllTheAmountfNumberWithString:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    /*
     * 不能输入.0-9以外的字符。
     * 设置输入框输入的内容格式
     * 只能有一个小数点
     * 小数点后最多能输入两位
     * 如果第一位是.则前面加上0.
     * 如果第一位是0则后面必须输入点，否则不能输入。
     */
   
    // 判断是否有小数点
    if ([textField.text containsString:@"."]) {
        isHaveDian = YES;
    }else{
        isHaveDian = NO;
    }
    
    if (string.length > 0) {
        
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        
        // 不能输入.0-9以外的字符
        if (!((single >= '0' && single <= '9') || single == '.'))
        {
            return NO;
        }
        
        // 只能有一个小数点
        if (isHaveDian && single == '.') {
            return NO;
        }
        
        // 如果第一位是.则前面加上0.
        if ((textField.text.length == 0) && (single == '.')) {
            textField.text = @"0";
        }
        
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if ([textField.text hasPrefix:@"0"]) {
            if (textField.text.length > 1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
                if (![secondStr isEqualToString:@"."]) {
                    return NO;
                }
            }else{
                if (![string isEqualToString:@"."]) {
                    return NO;
                }
            }
        }
        
        // 小数点后最多能输入两位
        if (isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            if (range.location > ran.location) {
                if ([textField.text pathExtension].length > 1) {
                    return NO;
                }
            }
        }
        if ([textField.text floatValue]>maxFloat) {
            return NO;
        }
        
    }
    return YES;
}

+ (BOOL)controllTheAmountfNumberWithString:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string maxDecimalLength:(NSInteger)length{
    /*
     * 不能输入.0-9以外的字符。
     * 设置输入框输入的内容格式
     * 只能有一个小数点
     * 小数点后最多能输入两位
     * 如果第一位是.则前面加上0.
     * 如果第一位是0则后面必须输入点，否则不能输入。
     */
    if (!length && string.length > 0) {
        return ![string isEqualToString:@"."] && [textField.text floatValue]<maxFloat;
    }
    // 判断是否有小数点
    if ([textField.text containsString:@"."]) {
        isHaveDian = YES;
    }else{
        isHaveDian = NO;
    }
    
    if (string.length > 0) {
        
        //当前输入的字符
        unichar single = [string characterAtIndex:0];
        
        // 不能输入.0-9以外的字符
        if (!((single >= '0' && single <= '9') || single == '.'))
        {
            return NO;
        }
        
        // 只能有一个小数点
        if (isHaveDian && single == '.') {
            return NO;
        }
        
        // 如果第一位是.则前面加上0.
        if ((textField.text.length == 0) && (single == '.')) {
            textField.text = @"0";
        }
        
        // 如果第一位是0则后面必须输入点，否则不能输入。
        if ([textField.text hasPrefix:@"0"]) {
            if (textField.text.length > 1) {
                NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
                if (![secondStr isEqualToString:@"."]) {
                    return NO;
                }
            }else{
                if (![string isEqualToString:@"."]) {
                    return NO;
                }
            }
        }
        
        // 小数点后最多能输入两位
        if (isHaveDian) {
            NSRange ran = [textField.text rangeOfString:@"."];
            if (range.location > ran.location) {
                if ([textField.text pathExtension].length > length-1) {
                    return NO;
                }
            }
        }
        if ([textField.text floatValue]>maxFloat) {
            return NO;
        }
        
    }
    return YES;
}
+ (NSString *)getRandomStringWithNum:(NSInteger)num
{
    NSString *string = [[NSString alloc]init];
    for (int i = 0; i < num; i++) {
        int number = arc4random() % 36;
        if (number < 10) {
            int figure = arc4random() % 10;
            NSString *tempString = [NSString stringWithFormat:@"%d", figure];
            string = [string stringByAppendingString:tempString];
        }else {
            int figure = (arc4random() % 26) + 97;
            char character = figure;
            NSString *tempString = [NSString stringWithFormat:@"%c", character];
            string = [string stringByAppendingString:tempString];
        }
    }
    return string;
}

+ (BOOL)controllTeleNumberWithString:(NSString *)text inRange:(NSRange)range inputString:(NSString *)string
{
    if ([string length]>0)
    {
        unichar single=[string characterAtIndex:0];//当前输入的字符
        if (text.length<11 && ( (single >='0' && single<='9')))//数据格式正确
        {
            //首字母只能为1
            if([text length]==0){
                if(single != '1'){
                    [text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                    
                }
            }
            
        }else{//输入的数据格式不正确
            [text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    
    return YES;
    
}

+ (BOOL)controllABC123WithString:(NSString *)text inRange:(NSRange)range inputString:(NSString *)string
{
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem])return NO;
    if (!string.length) {
        return YES;
    }
    if (![self isValidateABC123:string]) {
        [text stringByReplacingCharactersInRange:range withString:@""];
        return NO;
    }
    return YES;
}

//eos 账号名验证
+ (BOOL) validateEosAccountName:(NSString *)accountName{
    // 6位数字
    NSString *verifyAccountNameRegex = @"^[1-5a-z.]{1,12}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",verifyAccountNameRegex];
    return [predicate evaluateWithObject:accountName];
}
///是不是网站
+ (BOOL)isUrlAddress:(NSString*)url{
    NSString*reg =@"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSPredicate*urlPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reg];
    return[urlPredicate evaluateWithObject:url];
}


@end
